<?php include('fns_db_articles.php'); ?>
<?php include('../views/layouts/_header.php'); ?>
<?php include('../views/layouts/_left.php'); ?>
<?php //include('../check_if_admin.php');
if(!checkIfAdmin()){
		header("location: articles.php");
	}?>

<?php
function del_article_img($id){
	$result = get_article_by_id($id);
	$row = mysqli_fetch_assoc( $result );
	if($row['img_path'] != ''){
		unlink("../images/articles/thumbnails/".$row['img_path']);
		unlink("../images/articles/".$row['img_path']);
	}
}

function add_img_to_article($id){

	unset($imagename);

    if(!isset($_FILES) && isset($HTTP_POST_FILES))
    	$_FILES = $HTTP_POST_FILES;

    //print_r($_FILES);
    if(!isset($_FILES['img_file'])){
    	$error["img_file"] = "An image was not found.";
    	echo $error;
    	return;
    }

    $imagename = basename($_FILES['img_file']['name']);

    if(empty($error) && isset($imagename) && $imagename != null && $imagename != "")
    {
    	$newimage = "../images/" . $imagename;
    	$result = @move_uploaded_file($_FILES['img_file']['tmp_name'], $newimage);
    	if(empty($result)){
    		$error["result"] = "There was an error moving the uploaded file.";
    	}
    	resampimagejpg(900, 1200, $newimage, "../images/articles/".$imagename, 40);
    	resampimagejpg(300, 400, $newimage, "../images/articles/thumbnails/".$imagename, 20);
    	unlink($newimage);
    }else{
    	echo $error;

    }

    return $imagename;
}


if (isset($_GET['action']))
{
	$action = $_GET['action'];
	if ($action == '')
	{
		unset($action);
	}
}

switch ($action){
	case 'add':
	    $imagename = add_img_to_article(null);

		if (isset($_POST['title']))
		{
			$title = $_POST['title'];

			if ($title == '')
			{
				unset($title);
			}

		}

//		if (isset($_POST['date']))
//		{
//			$date = $_POST['date']; if ($date == '') {unset($date);}
//		}

		if (isset($_POST['description']))
		{
			$description = $_POST['description'];
//			if ($description == '')
//			{
//				unset($description);
//			}
		}

		if (isset($_POST['articleText']))
		{
			//$articleText = htmlentities($_POST['articleText']);
			$articleText = $_POST['articleText'];
//			$articleText = mysql_real_escape_string($articleText );
//			if ($articleText == '')
//			{
//				unset($articleText);
//			}
		}

		//$articleText = stripslashes( $_POST['FCKeditor1'] ) ;

		if (isset($_POST['author']))
		{
			$author = $_POST['author'];
			if ($author == '')
			{
				unset($author);
			}
		}

		if (isset($title) && isset($articleText) && isset($author))
		{

			$result = insert_article($title, $description, $articleText, $author, $imagename);

			if ($result == 'true')
			{
				echo "<p>Article added successfully!</p>";
			}
			else
			{
				echo "<p>Article not added!</p>";
				printf("Errormessage: %s\n", mysqli_error($cxn));
			}
		}
		else

		{
			echo "<p>Not all fields are filled. Article cannot be added.</p>";
		}

		break;


	case 'edit':
		if (isset($_POST['id']))
		{
			$id = $_POST['id'];//
		}

		if (isset($_POST['title']))
		{
			$title = $_POST['title'];
			if ($title == '')
			{
				unset($title);
			}
		}

		if (isset($_POST['description']))
		{
			$description = $_POST['description'];
//			if ($description == '')
//			{
//				unset($description);
//			}
		}

//		if (isset($_POST['date']))
//		{
//			$date = $_POST['date'];
//			if ($date == '')
//			{
//				unset($date);
//			}
//		}

		if (isset($_POST['author']))
		{
			$author = $_POST['author'];
			if ($author == '')
			{
				unset($author);
			}
		}

		if (isset($_POST['articleText']))
		{
			//$articleText = htmlentities($_POST['articleText']);
			$articleText = $_POST['articleText'];
//			$articleText = mysql_real_escape_string($articleText );
//			if ($articleText == '')
//			{
//				unset($articleText);
//			}
		}

		$imagename = add_img_to_article($id);

		//print_r($_POST);
//		logToFile("title: ".$title);
//		logToFile("articleText: ".$articleText);
//		logToFile("author: ".$author);
		if (isset($title) && isset($articleText) && isset($author))
		{
			if(isset($imagename) && $imagename != null){
				del_article_img($id);
				$result = update_article_with_imagename($title, $description, $articleText, $author, $imagename, $id);
			}else{
				$result = update_article_without_imagename($title, $description, $articleText, $author, $id);
			}

			if ($result == 'true')
			{
				echo "<p>Article updated successfully!</p>";
			}
			else
			{
				echo "<p>Article not updated!</p>";
			}
		}
		else
		{
			echo "<p>You didn't enter information and the article cannot be updated.</p>";
		}

		break;


	case 'del':
		if (isset($_GET['id']))
		{
			$id = $_GET['id'];
		}

		if (isset($id))
		{
			del_article_img($cxn, $id);

			$result = delete_article($id);

			if ($result == 'true') {echo "<p>Article deleted successfully!</p>";}
			else {echo "<p>Article not deleted!</p>";}
		}
		else
		{
			echo "<p>No article for deletion selected.</p>";
		}

		break;

	default:
		echo "<p>Unexpected parameter passed!</p>";
}
?>

<?php include('../views/layouts/_footer.php'); ?>